package emd24.telstudyresources;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;



import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * This class represents the screen where the user answers a question. It consists
 * of a question, five answers for each questions, buttons to navigate between
 * questions, and a marking feature
 * 
 * @author Evan Dyke
 *
 */
public class QuestionActivity extends Activity {

	private ToggleButton[] answerButtons;
	private TextView questionText;
	private Quiz quiz;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);

		this.questionText = (TextView) this.findViewById(R.id.questionText);
		ToggleButton[] answerButtons = {(ToggleButton) this.findViewById(R.id.choiceA), 
				(ToggleButton) this.findViewById(R.id.choiceB), (ToggleButton) this.findViewById(R.id.choiceC),
				(ToggleButton) this.findViewById(R.id.choiceD), (ToggleButton) this.findViewById(R.id.choiceE)};
		this.answerButtons = answerButtons;

		// Import Quiz


		String s = null;
		try {
			this.quiz = new Quiz(this.getJSONFromFile(R.raw.quiz));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			s = e.getMessage();
		} catch (IOException e) {
			finish();
		}
		
		if(this.quiz == null){
			return;
		}
		// Create button listeners
		for(int i = 0; i < this.answerButtons.length; i++){
			answerButtons[i].setOnClickListener(new OnClickListener() {
				public void onClick(View v){
					// TODO: Select the answer to the question
					select(v.getId());
				}
			});
		}
		this.findViewById(R.id.next).setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				setQuestion(quiz.getCurrQuestionIndex() + 1);
			}
		});
		this.findViewById(R.id.previous).setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				setQuestion(quiz.getCurrQuestionIndex() - 1);
			}
		});

		// Set question to UI
		setQuestion(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_question, menu);
		return true;
	}
	/**
	 * Changes the question displayed on the layout.
	 * 
	 * @param index of question to set
	 */
	private void setQuestion(int index){
		// Check to see if valid index		
		if(index >= 0 && index < this.quiz.getNumQuestions()){

			// Store answer to current question
			for(int i = 0; i < this.answerButtons.length; i++){
				if(this.answerButtons[i].isChecked()){
					this.quiz.getCurrentQuestion().setSelectedAnswer(i);
				}
			}
			this.quiz.getCurrentQuestion().setMarked(((ToggleButton)this.findViewById(R.id.mark)).isChecked());

			// Load the new question to the screen
			Question question = this.quiz.getQuestionAt(index);
			this.quiz.setCurrQuestionIndex(index);
			this.questionText.setText(question.getQuestionText());
			for(int choice = 0; choice < this.answerButtons.length; choice++){
				this.answerButtons[choice].setTextOn(question.getAnswers()[choice]);
				this.answerButtons[choice].setTextOff(question.getAnswers()[choice]);
				this.answerButtons[choice].setText(question.getAnswers()[choice]);
				this.answerButtons[choice].setChecked(false);
			}
			if(this.quiz.getCurrentQuestion().getSelectedAnswer() != -1){
				this.answerButtons[this.quiz.getCurrentQuestion().getSelectedAnswer()].setChecked(true);
			}

			// If the question was marked, indicate it on the screen
			((ToggleButton)this.findViewById(R.id.mark)).setChecked(this.quiz.getCurrentQuestion().isMarked());

		}else{
			return;	
		}

	}
	/**
	 * Checks the button with a given ID and deselects all other
	 * buttons
	 * 
	 * @param id int id of ToggleButton to select
	 */
	private void select(int id){

		for(int i = 0; i < this.answerButtons.length; i++){
			if(this.answerButtons[i].getId() != id){
				this.answerButtons[i].setChecked(false);
			}
			else{
				this.answerButtons[i].setChecked(true);
			}
		}
	}
	/**
	 * Gets a JSONObject from a text file resource.
	 * 
	 * @param filename String representing name of text file 
	 * @throws IOException 
	 * @throws JSONException 
	 */
	private JSONObject getJSONFromFile(int id) throws IOException, JSONException{
		InputStream is = this.getResources().openRawResource(R.raw.quiz);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
		String jsString = "";
		String line = null;
		while((line = reader.readLine()) != null){
			jsString += line;


		}
		is.close();
		reader.close();	
		return new JSONObject(jsString);
	}
}
